//
//  BLVideoView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/18.
//

#import "BLVideoView.h"
#import <Photos/Photos.h>

@interface BLVideoView ()
@property(nonatomic,assign) CGFloat videoW;
@property(nonatomic,assign) CGFloat videoH;
@property(nonatomic,strong) UIButton * playBtn;
@property(nonatomic,strong) UIView * baseView;
@property(nonatomic,strong) UIImageView * coverImageView;
//@property(nonatomic,strong)
@end

@implementation BLVideoView

-(instancetype)init{
    if (self = [super init]) {
    }
    return self;
}


-(UIImageView *)coverImageView{
    if (_coverImageView ==nil) {
        UIImageView * coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        coverImage.backgroundColor = [UIColor chColorWithHexString:@"#1775FF"];
        coverImage.userInteractionEnabled = YES;
        [self addSubview:coverImage];
        _coverImageView = coverImage;
    }
    return _coverImageView;
}


-(UIButton *)playBtn{
    if (_playBtn == nil) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playBtn addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:_playBtn];
    }
    return _playBtn;
}

-(UIView *)baseView{
    if (_baseView == nil) {
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _baseView.backgroundColor = [UIColor chColorWithHexString:@"#1775FF"];
        [self addSubview:_baseView];
    }
    return _baseView;
}

-(void)removeAll{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}


-(void)setVideoShow:(BOOL)videoShow{
    _videoShow = videoShow;
}


-(CGFloat)getVideoViewHWith:(URLModel *)model{
    
    _videoInfo = model;
    self.coverImageView.tag = kPlayerViewTag;
    
    CGFloat height = [model.height floatValue];
    CGFloat width = [model.width floatValue];
    CGFloat scale = height / width;
    CGFloat window_W = BL_WINDOW_WIDTH - 30;
    
    if (height <= width) {
        if (width > window_W) {
            width = window_W;
        }
        height = width * scale;
        
    }else{
        if (height > window_W) {
            height = window_W;
        }
        width =  height / scale;
    }
    self.coverImageView.x = (window_W - width)/2;
    self.coverImageView.width = width;
    self.coverImageView.height = height;
    
    NSURL * url = [NSURL URLWithString:model.momentUrl];
   [self getVideoPreViewImage:url];
    
    
    UIButton * playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.frame = CGRectMake((width - 50)/2, (height -50)/2, 50, 50);
    [playBtn setImage:[UIImage imageNamed:@"viewPlay"] forState:UIControlStateNormal];
    [self.coverImageView addSubview:playBtn];
    [playBtn addTarget:self action:@selector(playView:) forControlEvents:UIControlEventTouchUpInside];
    return height;
}

- (void)getVideoPreViewImage:(NSURL *)path
{

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
       dispatch_async(globalQueue, ^{
           AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
           AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
           assetGen.appliesPreferredTrackTransform = YES;
           CMTime time = CMTimeMakeWithSeconds(0.0, 600);
           NSError *error = nil;
           CMTime actualTime;
           CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
           UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
           CGImageRelease(image);
           
           dispatch_queue_t mainQueue = dispatch_get_main_queue();
            // 在主队列执行任务
            dispatch_async(mainQueue, ^{
                _coverImageView.image = videoImage;
            });
       });
}

-(void)playView:(UIButton *)sender{
    if (_playBlock) {
        _playBlock();
    }
}

-(void)setVideoInfo:(URLModel *)videoInfo{
    _videoInfo = videoInfo;
    CGFloat height = [videoInfo.height floatValue];
    CGFloat width = [videoInfo.width floatValue];
    CGFloat scale = height / width;
    CGFloat window_W = BL_WINDOW_WIDTH - 30;
    
    if (height <= width) {
        if (width > window_W) {
            width = window_W;
        }
        height = width * scale;
        
    }else{
        if (height > window_W) {
            height = window_W;
        }
        width =  height / scale;
    }
    self.baseView.width = width;
    self.baseView.height = height;
}


@end
